﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

using ApproveFlowEngine.Enums.Core;

using Volo.Abp.Application.Dtos;

namespace ApproveFlowEngine.Approve.DTO
{
    public class WorkFlowDto:EntityDto<Guid>
    {
        #region Public Properties

        public ApproveType ApproveType { get; set; }

        [MaxLength(255)]
        public string FlowName { get; set; }

        public ApproveFlowStatus FlowStatus { get; set; }

        public long FlowNo { get; set; }
        public bool IsDraft { get; set; }
        public List<WorkNodeDto> Nodes { get; set; } = new List<WorkNodeDto>();
        public List<WorkNodeDto> NodeTree { get; set; } = new List<WorkNodeDto>();

        [MaxLength(1024)]
        public string? Remark { get; set; }

        #endregion Public Properties



        #region Private Methods

        private List<WorkNodeDto> BuildNodeTree(List<WorkNodeDto> nodes, Guid? parentId)
        {
            var result = new List<WorkNodeDto>();
            var children = nodes.Where(c => c.ParentId == parentId);
            foreach (var child in children)
            {
                child.Children = BuildNodeTree(nodes, child.Id);
                result.Add(child);
            }
            return result;
        }

        #endregion Private Methods
        public void InitTree()
        {
            NodeTree = BuildNodeTree(Nodes, null);
        }
    }
}